package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.KepushoucangAdapter;
import com.jixiantubu.bean.Collectbean;
import com.jixiantubu.bean.Sciencebean;
import com.jixiantubu.jinnang.JinNang_KePu_NeiRongJianJie;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class KepushoucangActivity extends Activity {
	ListView kepushoucang_lv;
	// 数据来源
	List<Sciencebean> list = new ArrayList<Sciencebean>();
	HttpUtils httpUtils;
	KepushoucangAdapter adapter;
	MyApplication application;
	ImageView shezhifanhui;
	Collectbean collectbean;

	Intent intent;
	String username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_kepushoucang);
		intent = getIntent();
		username = intent.getStringExtra("username");
		Log.e("KepushoucangActivity------->", username);
		initViews();
		initData();
	}

	private void initViews() {
		shezhifanhui = (ImageView) findViewById(R.id.wo_shezhi);

		kepushoucang_lv = (ListView) findViewById(R.id.kepushoucang_lv);
		kepushoucang_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(KepushoucangActivity.this,
						JinNang_KePu_NeiRongJianJie.class);
				intent.putExtra("id", list.get(position));
				startActivity(intent);
			}
		});
	}

	public void fanhui(View v) {
		finish();
	}

	private void initData() {

		adapter = new KepushoucangAdapter(KepushoucangActivity.this, list);
		kepushoucang_lv.setAdapter(adapter);
		httpUtils = new HttpUtils();
		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/WoGuanzhu";
		RequestParams requParams = new RequestParams();
		
		requParams.addBodyParameter("key", "6");
		requParams.addBodyParameter("username", username);
		Log.e("收藏的key6---------", username);
		httpUtils.send(HttpMethod.POST, url, requParams,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// 获取服务器端返回的数据结果
						String result = arg0.result;
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<Sciencebean>>() {
						}.getType();
						List<Sciencebean> list1 = gson
								.fromJson(result, typeOfT);
						list.addAll(list1);
						Log.e("hua-eee-----", list.toString());
						adapter.notifyDataSetChanged();
					}

				});

	}
}
